package com.heliang.protocol.service;

import java.io.IOException;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.heliang.common.StringUtils;
import com.heliang.dataaccess.bean.AdvBean;
import com.heliang.protocol.baseBean.ReqBodyBaseBean;
import com.heliang.protocol.baseBean.RspBodyBaseBean;
import com.heliang.protocol.requestBean.ReqBodyAdvertisementBean;
import com.heliang.protocol.responseBean.RspBodyAdvertisementBean;
import com.heliang.util.Constants;

public class AdvertisementProtocolImpl extends ProtocolBase{
	/**
	 * 网络请求处理
	 * @param reqBodyBaseBean
	 * @param url //接口地址
	 * @return
	 * @throws IOException
	 * @throws JSONException
	 */
	public static RspBodyBaseBean dataProcess(ReqBodyBaseBean reqBodyBaseBean,
			String url) throws IOException, JSONException {
		String rspData = "";
		//封装请求对象
		ReqBodyAdvertisementBean reqBodyAdvertisementBean = (ReqBodyAdvertisementBean) reqBodyBaseBean;
		JSONObject reqJsonObject = new JSONObject();
		
		reqJsonObject.putOpt("keyvalue", reqBodyAdvertisementBean.getKeyValue());
		reqJsonObject.putOpt("ver", reqBodyAdvertisementBean.getVerAdv());
		reqJsonObject.putOpt("site_id", reqBodyAdvertisementBean.getSiteId());
		
		String dataStr = reqJsonObject.toString();
		try {
			url += URLEncoder.encode(StringUtils.encodeBase64(dataStr.getBytes()), "utf-8");
			rspData = getDataByReqServer(url);
		} catch (Exception e) {
			return null;
		}
		System.out.println("广告数据:"+rspData);
		
		//解析返回对象
		RspBodyAdvertisementBean rspBodyAdvertisementBean = new RspBodyAdvertisementBean();
		
		JSONObject rspJsonObject = new JSONObject(rspData);
		rspBodyAdvertisementBean.setVerAdv(rspJsonObject.optInt("ver"));
		
		JSONArray topArray = rspJsonObject.optJSONArray("tops");
		JSONArray footArray = rspJsonObject.optJSONArray("foots");
		
		String currentTimeStr = System.currentTimeMillis() + "";
		String pathStr = Constants.SDCARD_PATH + Constants.PATH_INFO;
		String picName = "";
		
		//顶部
		if (topArray != null && topArray.length() > 0) {
			for (int i = 0; i < topArray.length(); i++) {
				
				AdvBean advBean = new AdvBean();
				advBean.setId(topArray.getJSONObject(i).optInt("id"));
				advBean.setImgUrl(topArray.getJSONObject(i).optString("img"));
				advBean.setDec(topArray.getJSONObject(i).optString("desc"));
				advBean.setUrl(topArray.getJSONObject(i).optString("url"));
				advBean.setSort_id(topArray.getJSONObject(i).optInt("order"));
				picName = "";
				if (advBean.getImgUrl() != null && !advBean.getImgUrl().equals("")) {
					picName = "top_" + advBean.getId() + "_" + currentTimeStr + ".png";
				}
				advBean.setImgPath(pathStr + picName);
				rspBodyAdvertisementBean.getTops().add(advBean);
			}
		}
		
		//底部广告
		if (footArray != null && footArray.length() > 0) {
			for (int i = 0; i < footArray.length(); i++) {
				
				AdvBean advBean = new AdvBean();
				advBean.setId(footArray.getJSONObject(i).optInt("id"));
				advBean.setImgUrl(footArray.getJSONObject(i).optString("img"));
				advBean.setUrl(footArray.getJSONObject(i).optString("url"));
				advBean.setSort_id(footArray.getJSONObject(i).optInt("order"));
				picName = "";
				if (advBean.getImgUrl() != null && !advBean.getImgUrl().equals("")) {
					picName = "foot_" + advBean.getId() + "_" + currentTimeStr + ".png";
				}
				advBean.setImgPath(pathStr + picName);
				rspBodyAdvertisementBean.getFoots().add(advBean);
			}
		}
		
		JSONArray publishedtopArray = rspJsonObject.optJSONArray("publishedtop");
		JSONArray publishedfootArray = rspJsonObject.optJSONArray("publishedfoot");
		
		if (publishedtopArray != null && publishedtopArray.length() > 0) {
			int[] ids = new int[publishedtopArray.length()];		
			for (int i = 0; i < publishedtopArray.length(); i++) {
				ids[i] = publishedtopArray.getJSONObject(i).optInt("id");
			}
			rspBodyAdvertisementBean.setPublishedtop(ids);
		}
		
		if (publishedfootArray != null && publishedfootArray.length() > 0) {
			int[] ids = new int[publishedfootArray.length()];		
			for (int i = 0; i < publishedfootArray.length(); i++) {
				ids[i] = publishedfootArray.getJSONObject(i).optInt("id");
			}
			rspBodyAdvertisementBean.setPublishedfoot(ids);
		}
		
		return rspBodyAdvertisementBean;
	}
	
	
}
